package com.cqeec.waimai4.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqeec.waimai4.common.R;
import com.cqeec.waimai4.entity.Grade;
import com.cqeec.waimai4.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Currency;
import java.util.List;

@RestController
@RequestMapping("/grade")
public class GradeController {
    @Autowired
    GradeService gradeService;
    @PostMapping("getList")
    public R<List<Grade>> getList(){
        LambdaQueryWrapper<Grade> wrapper=new LambdaQueryWrapper<>();
        wrapper.gt(Grade::getJava,70).gt(Grade::getVue,90).orderByDesc(Grade::getPython);
        List<Grade> list = gradeService.list(wrapper);
        return R.success(list);
    }
    @PostMapping("getList2")
    public R<List<Grade>> getList2(){
        LambdaQueryWrapper<Grade> wrapper=new LambdaQueryWrapper<>();
        wrapper.gt(Grade::getPython,80).or()
                .gt(Grade::getJava,80).orderByDesc(Grade::getId);
        List<Grade> list=gradeService.list(wrapper);
        return R.success(list);
    }
    // RequestBody json类型数据转为实体类对象
    @PostMapping("addGrade")
    public R<String> addGrade(@RequestBody Grade grade){
        boolean save = gradeService.save(grade);
        if(save){
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }
    //将id为5的数据Java修改为100，python修改为95
    @PostMapping("updateGrade")
    public R<String> update(){
        //update grade set java=100,python=95 where id=5
        LambdaUpdateWrapper<Grade> wrapper=new LambdaUpdateWrapper<>();
        wrapper.set(Grade::getJava,100).set(Grade::getPython,95).eq(Grade::getId,5);
        boolean update = gradeService.update(wrapper);
        if (update) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    @GetMapping("/getPage/{current}/{size}")
    public R<Page<Grade>> getPage(@PathVariable int current,
                                 @PathVariable int size){
        Page<Grade> page=new Page<>(current,size);
        LambdaUpdateWrapper<Grade> wrapper=new LambdaUpdateWrapper<>();
        wrapper.orderByDesc(Grade::getId);
        Page<Grade> page1=gradeService.page(page,wrapper);
        return R.success(page1);
    }
}
